package com.hebeu.gfh.config;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class CorsConfig {

    @Bean
    public FilterRegistrationBean corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        /*是否允许请求带有验证信息*/
        config.setAllowCredentials(true);
        /*允许所有域名进行跨域调用*/
        config.addAllowedOriginPattern("*");
//        config.addAllowedOrigin("https://www.heustudent.xyz");
        /*允许服务端访问的客户端请求头*/
        config.addAllowedHeader("*");
        /*允许访问的方法名,GET POST等*/
        config.addAllowedMethod("*");
        source.registerCorsConfiguration("/**",config);
        FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
        bean.setOrder(0);
        return bean;
    }

}
